#include <iostream>

using namespace std;

class MyArray {
	public:
		MyArray() : m_p(NULL), m_nmemb(0) {}
		MyArray(int nmemb);
		~MyArray();
	
		int &operator[](int i);
		const int &operator[](int i) const;
		friend ostream &operator<< (ostream &out, const MyArray &a);

	private:
		int *m_p;
		int m_nmemb;
};

MyArray::MyArray(int nmemb)
{
	m_nmemb = nmemb;

	m_p = new int[nmemb];
}

MyArray::~MyArray()
{
	delete[] m_p;
}

int &MyArray::operator[](int i)
{
	return m_p[i];
}

const int &MyArray::operator[](int i) const
{
	return m_p[i];
}

// const MyArray
ostream &operator<< (ostream &out, const MyArray &a)
{
	int i;

	for (i = 0; i < a.m_nmemb; i++) {
		// 调用的是带const的operator[]
		out << a[i] << " ";
	}
	return out;
}

int main(void)
{
	MyArray a(10);
	int i;

	for (i = 0; i < 10; i++) {
		a[i] = i + 1;
	}

	cout << a << endl;

	return 0;
}


